﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Logic.Functions;
using System.ComponentModel;

namespace Logic
{
    public class Network : INotifyPropertyChanged
    {
        private float[] inputSignals;
        public int InputLayerSize 
        {
            get
            {
                return inputSignals.Count();
            }
            set
            {
                inputSignals = new float[value];
            }
        }
        public float[] InputSignals
        {
            get
            {
                return inputSignals;
            }

            set
            {
                if (value.Length != InputLayerSize)
                    throw new ArgumentException("Table of input weights has wrong size.");
                inputSignals = value;
            }
        }

        public BaseLayer[] Layers { get; set; }
        public int LayersCount
        {
            get { return Layers.Count(); }
            set
            {
                Layers = new BaseLayer[value];
            }
        }

        public Network()
        {
            LayersCount = 0;
            InputLayerSize = 0;
        }

        public static Network LoadNetwork(String path)
        {
            var network = new Network();
            String[] lines = File.ReadLines(path).ToArray();
            int layersCount = int.Parse(lines[0]);
            network.Layers = new BaseLayer[layersCount];
            int inputsCount = int.Parse(lines[1]);
            network.InputLayerSize = inputsCount;
            int layerId = 0;
            int lineNo = 2;
            int prevLayerSize = inputsCount;
            while (layerId < layersCount)
            {
                String type = lines[lineNo++];
                int neuronsCount;
                BaseLayer currentLayer;
                String[] tmp = lines[lineNo++].Split();
                if (type.Equals("LinearLayer"))
                {
                    var layer = new Layer();
                    currentLayer = layer;
                    neuronsCount = int.Parse(tmp[0]);
                    layer.Neurons = new Neuron[neuronsCount];
                    layer.ActivationFunction = ResourceManager.GetActivationFunctionBySymbol(tmp[1]);
                    for (int neuronId = 0; neuronId < neuronsCount; neuronId++)
                    {
                        var neuron = new Neuron();
                        String[] numbers = lines[lineNo++].Split();
                        neuron.Weights = new float[prevLayerSize];
                        for (int j = 0; j < prevLayerSize; j++)
                            neuron.Weights[j] = float.Parse(numbers[j]);
                        neuron.Bias = float.Parse(numbers[prevLayerSize]);
                        layer.Neurons[neuronId] = neuron;
                    }
                    
                }
                else if (type.Equals("KohonenLayer"))
                {
                    int rows = int.Parse(tmp[0]);
                    int columns = int.Parse(tmp[1]);
                    var layer = new KohonenLayer(rows, columns, prevLayerSize);
                    currentLayer = layer;
                    layer.ActivationFunction = ResourceManager.GetActivationFunctionBySymbol(tmp[2]);
                    if (tmp.Length < 4)
                    {
                        for (int i = 0; i < rows; i++)
                            for (int j = 0; j < columns; j++)
                            {
                                var neuron = new Neuron();
                                String[] numbers = lines[lineNo++].Split();
                                neuron.Weights = new float[prevLayerSize];
                                for (int k = 0; k < prevLayerSize; k++)
                                    neuron.Weights[k] = float.Parse(numbers[k]);
                                layer.Neurons[i][j] = neuron;
                            }
                    }
                    else if (tmp[3].Equals("Random"))
                            layer.InitializeWeights(float.Parse(tmp[4]), float.Parse(tmp[5]));
                    // else - weights = 0
                }
                else if (type.Equals("BpLayer"))
                {
                    var layer = new BpLayer();
                    currentLayer = layer;
                    neuronsCount = int.Parse(tmp[0]);
                    layer.Neurons = new Neuron[neuronsCount];
                    layer.ActivationFunction = ResourceManager.GetActivationFunctionBySymbol(tmp[1]);
                    var bias = float.Parse(tmp[2]);
                    for (int neuronId = 0; neuronId < neuronsCount; neuronId++)
                    {
                        var neuron = new Neuron(prevLayerSize);
                        neuron.Bias = bias;
                        layer.Neurons[neuronId] = neuron;
                    }
                    layer.InitializeWeights(-1, 1); //inicjalizacja losowymi wartościami
                }
                else
                {
                    neuronsCount = int.Parse(tmp[0]);
                    var layer = new GrossbergLayer(neuronsCount, prevLayerSize);
                    currentLayer = layer;
                    layer.ActivationFunction = ResourceManager.GetActivationFunctionBySymbol(tmp[1]);
                    layer.WH = "WH".Equals(tmp[2]);
                    layer.InitializeWeights(-1, 1); //inicjalizacja losowymi wartościami
                }

                network.Layers[layerId++] = currentLayer;
                prevLayerSize = currentLayer.Size;
            }
            return network;
        }

        public void Process()
        {
            float[] signals = InputSignals;
            foreach (BaseLayer layer in Layers)
            {
                layer.ProcessNeurons(signals);
                signals = layer.GetOutputSignals();
            }
        }

        public float[] GetOutputSignals()
        {
            return Layers[Layers.Length - 1].GetOutputSignals();
        }

        public void Save(String path)
        {
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(Layers.Length);
                sw.WriteLine(InputLayerSize);
                foreach (var layer in Layers)
                    layer.Save(sw);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(String info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}
